package com.guli.product.service.impl;

import com.guli.product.dao.PmsAttrGroupDao;
import com.guli.product.dao.PmsCategoryDao;
import com.guli.product.entity.PmsAttrGroupEntity;
import com.guli.product.entity.PmsCategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.product.dao.PmsAttrDao;
import com.guli.product.entity.PmsAttrEntity;
import com.guli.product.service.PmsAttrService;


@Service("pmsAttrService")
public class PmsAttrServiceImpl extends ServiceImpl<PmsAttrDao, PmsAttrEntity> implements PmsAttrService {

    @SuppressWarnings("all")
    @Autowired
    PmsAttrDao attrDao;

    @SuppressWarnings("all")
    @Autowired
    PmsCategoryDao categoryDao;

    @SuppressWarnings("all")
    @Autowired
    PmsAttrGroupDao attrGroupDao;

    @Override
    public boolean save(PmsAttrEntity entity) {
        PmsCategoryEntity cate = categoryDao.selectById(entity.getCatelogId());
        if (entity.getAttrGroupId() != null) {
            PmsAttrGroupEntity group = attrGroupDao.selectById(entity.getAttrGroupId());
            entity.setAttrGroupName(group.getAttrGroupName());
        }
        entity.setCatelogName(cate.getName());
        return super.save(entity);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, String type, Long catelogid) {
        QueryWrapper<PmsAttrEntity> wrapper = new QueryWrapper<>();
        if ("base".equals(type)) {
            wrapper.eq("attr_type", 1);
        } else {
            wrapper.eq("attr_type", 0);
        }
        if (!catelogid.equals(0L)) {
            wrapper.eq("catelog_id", catelogid);
        }
        IPage<PmsAttrEntity> page = this.page(
                new Query<PmsAttrEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PmsAttrEntity findOne(Long attrId) {
        PmsAttrEntity pmsAttrEntity = attrDao.selectById(attrId);
        Long catelogId = pmsAttrEntity.getCatelogId();
        List<Long> longs = new ArrayList<>();
        take(longs, catelogId);
        Collections.reverse(longs);
        Long[] longs1 = longs.toArray(new Long[longs.size()]);
        pmsAttrEntity.setCatelogPath(longs1);
        return pmsAttrEntity;
    }

    @Override
    public PageUtils datalist(Map<String, Object> params, Long attrGroupId) {
        QueryWrapper<PmsAttrEntity> wrapper = new QueryWrapper<>();
        wrapper.isNull("attr_group_id");
        IPage<PmsAttrEntity> page = this.page(
                new Query<PmsAttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);

    }

    @Override
    public void uprelation(List<PmsAttrEntity> pmsAttr) {
        if (pmsAttr!=null){
            for (PmsAttrEntity pmsAttrs : pmsAttr) {
                PmsAttrEntity pmsAttrEntity = attrDao.selectById(pmsAttrs.getAttrId());
                pmsAttrEntity.setAttrGroupId(pmsAttrs.getAttrGroupId());
                PmsCategoryEntity categoryEntity = categoryDao.selectById(pmsAttrs.getAttrGroupId());
                pmsAttrEntity.setAttrGroupName(categoryEntity.getName());
                attrDao.updateById(pmsAttrEntity);
                System.out.println(pmsAttrEntity);
            }
        }

    }


    public void take(List<Long> list, Long catelogid) {
        list.add(catelogid);
        PmsCategoryEntity pmsCategoryEntity = categoryDao.selectById(catelogid);
        if (!pmsCategoryEntity.getParentCid().equals(0L)) {
            take(list, pmsCategoryEntity.getParentCid());
        }
    }

}